package org.banaan.metadata

import org.banaan.sql.{SQLConnection};
/*
 * Metadata structure, contains simple table structure and columns.
 * In time, should also contain relational contructs. We have taken
 * care to leave the values in the instants as immutable as possible,
 * i.e. vals.
*/
class Database {
	
	val TABLE_TYPE = "TABLE";
  
	def createDatabase(connection: SQLConnection): List[Table] = {
		var tableNames = getTableNames(connection, TABLE_TYPE)
		return tableNames map (x => new Table(x, getColumns(connection, x))) 
	}
 
	def getTableNames(connection: SQLConnection, tabletype: String) : List[String] = {
		connection.getTableNames(TABLE_TYPE);
	}
 
	/*
	* Had to do a nice bit of magic to get rid of the generic '?' type from java.
	* There is probably a more elegant way to do this. 
	*/
	def getColumns(connection: SQLConnection, tableName: String) : List[Column[_]] = {
		var abstractColumns = connection.getColumns(tableName)
		return abstractColumns map (x => new Column(x.getType,x.getName(),x.getLength(),x.getDefaultValue()))
	}
} 

class Table (val name:String, val columns:List[Column[_]]) {
	override def toString = "[Table: " + name + ", columns: " + columns + "]"
}

class Column[A](val columnType:Int, val name:String,val length:Int, val defaultExpression:String) {
	override def toString = "[Column: " + name + "]"
}